package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.UserDetail;
import com.ruoyi.system.service.IUserDetailService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户细节表增删改查Controller
 * 
 * @author hyb
 * @date 2023-03-03
 */
@RestController
@RequestMapping("/detail")
public class UserDetailController extends BaseController
{
    @Autowired
    private IUserDetailService userDetailService;

    /*
    * 获取用户个人信息
    * */
    @GetMapping("/personal")
    public AjaxResult getPersonal(){
        UserDetail userDetail = new UserDetail();
        userDetail.setUserId(SecurityUtils.getUserId());
        List<UserDetail> userDetails = userDetailService.selectUserDetailList(userDetail);

        return AjaxResult.success().put("user",userDetails.size()==0?"":userDetails.get(0));
    }


    @InnerAuth
    @GetMapping("/inner/personal")
    public R<UserDetail> getInnerPersonal(@RequestParam("userId")Long userId){
        UserDetail userDetail = new UserDetail();
        userDetail.setUserId(userId);
        List<UserDetail> userDetails = userDetailService.selectUserDetailList(userDetail);
        return R.ok(userDetails.size()==0?null:userDetails.get(0));
    }


    /**
     * 查询用户细节表增删改查列表
     */
    @RequiresPermissions("userDetail:detail:list")
    @GetMapping("/list")
    public TableDataInfo list(UserDetail userDetail)
    {
        startPage();
        List<UserDetail> list = userDetailService.selectUserDetailList(userDetail);
        return getDataTable(list);
    }

    /**
     * 导出用户细节表增删改查列表
     */
    @RequiresPermissions("userDetail:detail:export")
    @Log(title = "用户细节表增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserDetail userDetail)
    {
        List<UserDetail> list = userDetailService.selectUserDetailList(userDetail);
        ExcelUtil<UserDetail> util = new ExcelUtil<UserDetail>(UserDetail.class);
        util.exportExcel(response, list, "用户细节表增删改查数据");
    }

    /**
     * 获取用户细节表增删改查详细信息
     */
    @RequiresPermissions("userDetail:detail:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userDetailService.selectUserDetailById(id));
    }

    /**
     * 新增用户细节表增删改查
     */
    @RequiresPermissions("userDetail:detail:add")
    @Log(title = "用户细节表增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserDetail userDetail)
    {
        return toAjax(userDetailService.insertUserDetail(userDetail));
    }

    /**
     * 修改用户细节表增删改查
     */
    @RequiresPermissions("userDetail:detail:edit")
    @Log(title = "用户细节表增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserDetail userDetail)
    {
        return toAjax(userDetailService.updateUserDetail(userDetail));
    }

    /**
     * 删除用户细节表增删改查
     */
    @RequiresPermissions("userDetail:detail:remove")
    @Log(title = "用户细节表增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userDetailService.deleteUserDetailByIds(ids));
    }
}
